<?php

!defined('ADMIN') && exit('Not allowed to directly view page');

if(!isset($action)) $action = '';
if(empty($action))
{
	//问答分类
	$query = "select id, name, reid from `#@__asktype` order by disorder desc";
	$dsql->Execute('me',$query);
	$tids  = "var class_level_1=new Array( \n";
	$tid2s = "var class_level_2=new Array( \n";
	while($asktype = $dsql->getarray())
	{
		if($asktype['reid'] == 0)
		{
			$tids .= 'new Array("'.$asktype['id'].'","'.$asktype['name'].'"),'."\n";
		}else
		{
			$tid2s .= 'new Array("'.$asktype['reid'].'","'.$asktype['id'].'","'.$asktype['name'].'"),'."\n";
		}
	}
	$tids = substr($tids,0,-2)."\n";
	$tid2s = substr($tid2s,0,-2)."\n";
	$tids .= ');';
	$tid2s .= ');';
	if($id) {
		$sql = "select * from `#@__ask` where id=" .(int)$id;
		$askquestion = $dsql->getone($sql);
		$askanswer = $dsql->getone("select * from `#@__askanswer` where id=".$askquestion[bestanswer]);
		//status $kind=$kind==1?" `status`=1 ":($kind==2?" `status`=2 ":"`digest`=1");
		$k = array();
		if($askquestion['status'] == 1)
			$k['kind'] = 1;
		else if($askquestion['status'] ==2)
			$k['kind'] =2;
		else if($askquestion['digest'] ==1)
			$k['kind'] =3;

	}
	include(DEDEADMIN.'/templets/ask_answer_me.htm');

}
/*********
**ajax 医生
**
**
***********/
else if($action == "ajax")
{
	//医生
	AjaxHead();
	if(!$tid) Return false;
	$sql = "Select name,photo,id from `#@__doctor`";   //修改 2012-10-29
	$dsql->Execute('me',$sql);
	while($row = $dsql->getarray())
	{
		$doctors[] = $row;
	}

	$str =" <select onChange=\"javascript:docChange(this.options[selectedIndex]);\" name=\"doctorid\" >";
	$a = array();
	foreach($doctors as $key => $doctor){
		if($did == $doctor['id']){ $a[id] = $key;$a[photo] = $doctor[photo]; $a[s] = 'selected=\'selected\'';}
		$str.="<option value=\"".$doctor[id]."\" title=\"$doctor[photo]\" ".$a[s].">".$doctor[name]."</option>\n";
	}

	$str3 = !empty($a[id]) ? $a[photo] : $doctors[0]['photo'];
	$str2="<img width=\"75\" height=\"78\" src=\"".$str3."\" id=\"doc\" />\n";
	$str.="</select> ";
	echo $str2.$str;
}

/*********
**
**	提交数据 保存
**
***********/
else if($action == "add")
{
	$query = "select `id`,`name`,`reid` from `#@__asktype` where id='$ClassLevel1' or id='$ClassLevel2'";
	$dsql->Execute('me',$query);
	$tids=array();
	while($asktype = $dsql->getarray())
	{
		$tids[$asktype['id']]=$asktype['name'];
	}

	$solvetime=time();
	$asktime=time()-3600*24;
	$digest=$kind=="3"?1:0;
	$status=$kind=="3"?1:$kind;
	$uid = $cuserLogin->getUserID();
	$uname=$cuserLogin->getUserName();
	$ip = GetIP();

	$query="insert into `#@__ask`(tid, tidname, tid2, tid2name, uid, anonymous, status, digest,title, dateline, expiredtime, ip ,content, extra,replies) values ('$ClassLevel1', '$tids[$ClassLevel1]', '$ClassLevel2', '$tids[$ClassLevel2]', '$uid', '1', '$status',$digest,'$title', '$asktime', '$expiredtime', '$ip', '$content', '','1')";
	$dsql->ExecuteNoneQuery2($query);
	$lastid=$dsql->GetLastID();

	$query="insert into `#@__askanswer`  values ('',$lastid,1,'$ClassLevel1','$ClassLevel2','$uid','$uname','','','1','$ip','$solvetime','$brief','$answeraa',1,'$doctorid')";
	$yy = $dsql->ExecuteNoneQuery2($query);
	$lastid1 = $dsql->GetLastID();
	$yy = $dsql->ExecuteNoneQuery2("update `#@__ask` set bestanswer='$lastid1' where id = $lastid");
	ShowMsg("发表成功",'?do=ask_answer');

}else if($action =='edit'){
	$query = "select `id`,`name`,`reid` from `#@__asktype` where id='$ClassLevel1' or id='$ClassLevel2'";
	$dsql->Execute('me',$query);
	$tids=array();
	while($asktype = $dsql->getarray())
	{
		$tids[$asktype['id']]=$asktype['name'];
	}

	$solvetime=time();
	$asktime=time()-3600*24;
	$digest=$kind=="3"?1:0;
	$status=$kind=="3"?1:$kind;
	$uid = $cuserLogin->getUserID();
	$uname=$cuserLogin->getUserName();
	$query = "update `#@__ask` set tid='$ClassLevel1',tid2='$ClassLevel2',tidname='$tids[$ClassLevel1]',tid2name='$tids[$ClassLevel2]',status='$status',digest='$digest',title='$title',content='$content',replies=replies+1 where id =$id";
	$ip = GetIP();
	$dsql->ExecuteNoneQuery2($query);
	$lastid=$id;
	$answer = $dsql->getone("select * from `#@__ask` where id= $lastid");
	if ($answer['bestanswer'] <> 0)
	{
		$query="update `#@__askanswer` set tid='$ClassLevel1',tid2='$ClassLevel2',ifanswer='1',brief='$brief',content='$answeraa',doctorid='$doctorid' where id = $answer[bestanswer]";
		$dsql->ExecuteNoneQuery2($query);
	}
	else
	{
		$query="insert into `#@__askanswer`  (askid,tid,tid2,uid,username,anonymous,goodrate,badrate,userip,dateline,brief,content,ifcheck,doctorid)values ($lastid,'$ClassLevel1','$ClassLevel2','$uid','$uname','','','1','$ip','$solvetime','$brief','$answeraa',1,'$doctorid')";
		$dsql->ExecuteNoneQuery2($query);
		$lastid1 = $dsql->GetLastID();
		$dsql->ExecuteNoneQuery2("update `#@__ask` set bestanswer='$lastid1' where id = $lastid");
	}
	ShowMsg("评论成功",'?do=ask_answer_me&id='.(int)$id);
}

?>